<!-- 上传页面 -->
<template>
  <div style="overflow: auto; height: 100%">
    <el-upload
      class="upload-demo"
      action
      ref="upload"
      :on-change="onChange"
      :on-remove="handleRemove"
      :multiple="true"
      :file-list="fileList"
      :before-upload="handleBeforeUpload"
      :http-request="handleHttpRequest"
      list-type="picture"
      :auto-upload="false"
    >
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button
        style="margin-left: 10px"
        size="small"
        type="success"
        @click="submitUpload"
        >上传到服务器</el-button
      >
      <div slot="tip" class="el-upload__tip">上传jpg/png文件,不超过50MB</div>
      <div class="process">
        <el-progress
          v-show="progressFlag"
          status="success"
          :text-inside="true"
          :stroke-width="24"
          :percentage="progressPercent"
        ></el-progress>
      </div>
    </el-upload>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progressPercent: 0,
      fileList: [],
      fileCount: 0,
      alreayUp: 0,
      progressFlag: false,
    };
  },
  methods: {
    submitUpload() {
      //会调用handleHttpRequest
      this.fileCount = this.fileList.length;
      console.log("this.fileList.length", this.fileList.length);
      this.progressFlag = true;
      if (this.fileCount == 0) {
        this.progressFlag = false;
        this.$message({
          message: "请选择导入的文件",
          type: "warning",
          duration: "2000",
        });
      }
      this.$refs.upload.submit();
      //this.fileList = [];
    },
    handleBeforeUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isJPG2 = file.type === "image/jpg";
      const isPNG = file.type === "image/png";
      const isLt50M = file.size / 1024 / 1024 < 50;
      if (!isJPG && !isJPG2 && !isPNG) {
        this.$message.warning("只支持jpg或png格式图片");
      }
      if (!isLt50M) {
        this.$message.warning("请上传50MB以内的图片!");
      }
      return (isJPG || isJPG2 || isPNG) && isLt50M;
    },
    onChange(file, fileList) {
      this.fileList = fileList;
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    async handleHttpRequest(param) {
      //有一个文件就调用一次handleHttpRequest
      console.log("handleHttpRequest");
      let formdata = new FormData();
      formdata.append("images", param.file);
      let res = await this.$post("/imgcenter/uploadImage", formdata);
      let resMessage = res.message;
      if (res.success == true && resMessage.indexOf("上传完成") != -1) {
        // this.$notify({
        //   title: resMessage,
        //   type: "success",
        //   duration: 1500,
        // });
        param.onSuccess();
        setTimeout(() => {
          this.fileList.shift();
        }, 0);

        this.alreayUp++;
        console.log("3333");
        console.log(this.alreayUp, this.fileCount);
        if (this.alreayUp != this.fileCount) {
          this.progressPercent = parseInt(
            (this.alreayUp / this.fileCount) * 100
          );
        } else {
          this.progressPercent = 100;
          this.$notify({
            title: "上传完成",
            type: "success",
            duration: 2000,
          });
          setTimeout(() => {
            this.progressPercent = 0;
            this.progressFlag = false;
            this.alreayUp = 0;
          }, 1000);
        }
      } else {
        this.$notify({
          title: resMessage,
          type: "error",
          duration: 5000,
        });
        param.onError();
      }
    },
  },
};
</script>

<style scoped>
.upload-demo {
  margin-top: 10px;
  margin-left: 10px;
}
.process {
  margin-top: 10px;
  width: 100%;
}
</style>
